package action.admin.assignment;

import beans.Account;
import beans.Assignment;
import dao.CheckAccount;
import dao.MngAssignment;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class createAssign extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {

            String assign_name = request.getParameter("assign_name");
            String class_id = request.getParameter("class_id");
            String create_date = request.getParameter("create_date");
            String finish_date = request.getParameter("finish_date");
            CheckAccount ck = new CheckAccount();
            List<Account> list_student = ck.listAccByClassId(class_id);
            String msg = "";
            String error = "";
            MngAssignment mngAssign = new MngAssignment();
            if (assign_name != null && !assign_name.equals("")) {
                if (!mngAssign.checkAssignName(assign_name)) {
                    for (Account acc : list_student) {
                        Assignment assign = new Assignment();
                        assign.setAcc_id(acc.getAcc_id());
                        assign.setClass_id(class_id);
                        assign.setAssign_name(assign_name);
                        assign.setCreate_date(create_date);
                        assign.setFinish_date(finish_date);
                        mngAssign.createAssign(assign);
                    }
                    msg = "Create Assignment successful";
                    request.setAttribute("msg", msg);
                    request.setAttribute("class_id", class_id);
                    RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=showassign");
                    rd.forward(request, response);
                } else {
                    error = "Assignment name exist!";
                    request.setAttribute("error", error);
                    request.setAttribute("class_id", class_id);
                    RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=create_assign");
                    rd.forward(request, response);
                }
            } else {
                error = "Insert Assignment name !";
                request.setAttribute("error", error);
                request.setAttribute("class_id", class_id);
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=create_assign");
                rd.forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
